package com.smasher.camera.activity

import android.hardware.camera2.CameraManager
import android.os.Bundle
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity
import com.smasher.camera.databinding.ActivityCameraListBinding


class CameraListActivity : AppCompatActivity() {

    private val mBinding: ActivityCameraListBinding by lazy {
        createViewBinding()
    }

    fun createViewBinding(): ActivityCameraListBinding {
        return ActivityCameraListBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)
        val cameraIds: List<String> = queryCameraList()
        mBinding.spinner.adapter =
            ArrayAdapter(this, android.R.layout.simple_spinner_item, cameraIds)
    }

    private fun queryCameraList(): List<String> =
        runCatching {
            (getSystemService(CAMERA_SERVICE) as CameraManager).cameraIdList.toList()
        }.getOrElse {
            listOf()
        }

    companion object {
        const val TAG = "CameraListActivity"
    }
}